<?php
// +----------------------------------------------------------------------
// |     【StoreMain】Controller 控制器
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\admin\controller\store;

use think\facade\Session;
use think\facade\Request;
use app\common\service\StoreMain as S;
use app\common\model\StoreMain as M;
use app\common\service\SystemConfig as CONF_S;

class Main extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];

	//初始化
	public function initialize(){
		parent::initialize();
		
		$this->assign('controller_title','商户管理');
	}

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList());
        }
        return $this->fetch();
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
			$res=S::goAdd(Request::post());
			// 创建商铺配置记录
			CONF_S::goAdd(['uuid'=>$res['id']]);
            return $this->getJson();
        }
		
		// 默认有效期
		$default_exp=date("Y-m-d",strtotime("+1 month"));
        return $this->fetch('edit',[
			'action_up'=>false,
			'model'=>[
				'exp_time'=>$default_exp,
			]
		]);
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(),$id));
        }
        return $this->fetch('',[
			'action_up'=>true,
			'model' => M::info($id),
		]);
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post(),$id));
    }

    // 删除
    public function remove($id){
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove(){
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
    }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle());
        }
        return $this->fetch();
    }
	
    // 设置某个值
    public function setValue(){
        if (Request::isAjax()) {
            return $this->getJson(S::setFieldValue(Request::post()));
        }
        return $this->fetch();
    }
	
	// 设置后台当前值
	public function setID(){
		if (Request::isAjax()) {
			$get=Request::GET();
			$id=$get['id'];
			Session::set('admin.uuid', $id);
			return $this->getJson(['msg'=>'设置成功！']);
		}else{
			return $this->getJson(404);
		}
	}
}
